/**
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var searchForm_str = "<center><form><table cellpadding='0' cellspacing='0'><tr><td><input size='28' id='searchText' name='searchText' /></td><td><input type='submit' id='search_submit' value='Search'/></td></tr><tr><td colspan='2'><table cellpadding='0' cellspacing='0'><tr><td align='left' valign='top'><span class='poweredby'>powered by</span></td><td align='left' valign='bottom'><img src='http://www.google.com/uds/css/small-logo.png' width='51' height='15' /></td></tr></table></td></tr></table></form></center>";
var searchURL_tmpl = "http://www.google.com/codesearch?q=%SEARCH%+package:http://%PROJECT%.googlecode.com";
var url_tmpl = "http://code.google.com/p/%PROJECT%/";
var title_tmpl = "%PROJECT% code search";
var prefs_obj;
var projectName_str = "";

/**
 * Actually render the content of this gadget.
 *
 * @param response_str  The page retrieved by the common <code>init()</code>, unused.
 */
function render(response_str) {
  // Fill the Content div
  $("#content_div").html(searchForm_str);
  adjustIFrameHeight();

  // Add an event handler for the search box that calls a
  // new window at the appropriate search URL
  $("#search_submit").click(function(event) {
    event.preventDefault();
    performSearch();
  })

  // Previously IE's enter key caused the frame to fail.
  // The subverts the default event and runs our custom
  // submit function performSearch()
  $("#searchText").keypress(function(event) {
     if (event.which == 13) {
      event.preventDefault();
      performSearch();
    }
   });
}

/**
 * Called by search form events to open a new window with the proper search URL.
 */
function performSearch() {
  // Get the search text, load the
  var searchText_str = $("#searchText").val();
  if (searchText_str != "") {
    searchText_str = _esc(searchText_str).replace(/%20/g, "+");

    var searchURL_str = searchURL_tmpl.replace("%SEARCH%", searchText_str).replace("%PROJECT%", _esc(projectName_str));
    window.open(searchURL_str, "_blank");
  }
}

gadgets.util.registerOnLoadHandler(init);
